// status.js
var iconUrl = require('../../config.js').iconUrl;
var cmds=require('../../utils/cmd.js');
var blu = require('../../utils/blue.js');
Page({
  go_search:function(){
      var that=this;      
      wx.navigateTo({
        url: '/pages/search/search?page=1',
      })
  },
  heartrate:function(){
    if(!this.before()){
        return;
    }
    if (!this.before1()) {
      return;
    }    
    wx.navigateTo({
      url: '/pages/heartrate/heartrate',
    })
  },
 sleepdetail:function(){
      wx.navigateTo({
        url: '/pages/sleepdetail/sleepdetail',
      })
 },

  /**
   * 页面的初始数据
   */
  data: {
    iconUrl:iconUrl,
    pace:0,
    mileage:0,
    carorie:0 ,
    con_text:"未"      
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {   
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {    
    this.onPullDownRefresh();  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
     if(!this.before()){
       wx.hideNavigationBarLoading();
       wx.stopPullDownRefresh();  
      return false;
     }
    if (getApp().globalData.blue_is_connectd){
      var that=this;
      wx.showNavigationBarLoading();
      blu.blue_write(cmds.set6_2());
      setTimeout(function(){
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();     
        var calorie = wx.getStorageSync("bracelet_data.current_calorie");
        if(calorie.startsWith(":"))   {
          calorie=calorie.substr(1);
        }
        that.setData({
          pace: wx.getStorageSync("bracelet_data.current_pace"),
          mileage: wx.getStorageSync("bracelet_data.current_mileage"),
          carorie: calorie, 
        });        
      }, 2000);
      this.setData({
        con_text: "已"
      });
      return;
     }
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();    
    this.setData({
      con_text: "未"
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  before:function(){
    if (!getApp().globalData.blue_adapter_able) {
      wx.showToast({
        title: '请打开蓝牙！',
        icon: "loading"
      })
      wx.openBluetoothAdapter({
        success: function(res) {
          getApp().globalData.blue_adapter_able=true;
        },
      })
      return false;
    }else{
      return true;
    }
  },
  before1: function () {
    if (!getApp().globalData.blue_is_connectd) {
      wx.showToast({
        title: '设备未连接！',
      })
      return false;
    } else {
      return true;
    }
  },
})